package com.hu.controller;

import com.alibaba.fastjson.JSON;
import com.hu.mapper.MusicMapping;
import com.hu.pojo.Music;
import com.hu.utils.RestfulUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import static com.hu.config.LoginHandlerInterceptor.useIdLocal;


@Controller
public class MusicController {

    @Autowired
    MusicMapping musicMapping;

    @GetMapping("/music/getbyid/{musicId}")
    @ResponseBody
    public String getOne(@PathVariable Integer musicId){
        Music music = musicMapping.queryById(musicId);
        return RestfulUtil.toRestful(music);
    }

    @PostMapping("/music/getall")
    @ResponseBody
    public String getAll(){
        List<Music> musics = musicMapping.queryAll();
        return RestfulUtil.toRestful(musics);
    }

    @PostMapping("/music/insert")
    @ResponseBody
    public String insert(Music music){
        musicMapping.addMusic(music);
        return RestfulUtil.toRestful(null);
    }

    @GetMapping("/music/delete/{musicId}")
    @ResponseBody
    public String delete(@PathVariable Integer musicId){
        musicMapping.deleteMusic(musicId);
        return RestfulUtil.toRestful(null);
    }


    //增加播放记录接口
    @PostMapping("/music/playrecord/insert")
    @ResponseBody
    public String insertPlayRecord(Integer musicId){
        Integer userId = useIdLocal.get();
        if (userId == null){
            return RestfulUtil.toRestful(null,"用户id丢失",404);
        }
        Music music = musicMapping.queryById(musicId);
        if (music.getMusicBroadcastNum() == null || music.getMusicBroadcastNum() <= 0 ){
            music.setMusicBroadcastNum(1);
        }else {
            int num = music.getMusicBroadcastNum();
            music.setMusicBroadcastNum(num + 1);
        }
        musicMapping.updateMusic(music);
        musicMapping.insertPlayRecord(musicId,userId,new Date());
        return RestfulUtil.toRestful(null,"播放记录保存成功",200);
    }

    //查询当前用户历史播放
    @PostMapping("/music/playrecord/getall")
    @ResponseBody
    public String selectPlayRecord(){
        Integer userId = useIdLocal.get();
        if (userId == null){
            return RestfulUtil.toRestful(null,"用户id丢失",404);
        }
        List<Music> music = musicMapping.queryByPlayRecord(userId);
        Collections.reverse(music);
        return RestfulUtil.toRestful(music);
    }

    //根据播放的次数获取歌曲列表
    @PostMapping("/music/getbyplay")
    @ResponseBody
    public String getAllByPlayNum(){
        List<Music> musics = musicMapping.queryByPlayNum();
        return RestfulUtil.toRestful(musics);
    }

    //根据喜欢的次数获取歌曲列表
    @PostMapping("/music/getbylike")
    @ResponseBody
    public String getAllByLike(){
        List<Music> musics = musicMapping.queryByLikeNum();
        return RestfulUtil.toRestful(musics);
    }

    //用户个性化推荐
    @PostMapping("/music/getbyuser")
    @ResponseBody
    public String getAllByUser(){
        List<Music> musics = musicMapping.queryAll();
        Collections.shuffle(musics);
        List<Music> list = new ArrayList<>();
        if (list.size() > 30){
            for (int i = 0;i < 30;i++){
                list.add(musics.get(i));
            }
        } else {
            return RestfulUtil.toRestful(musics);
        }
        return RestfulUtil.toRestful(list);
    }

    //根据类型查询音乐
    @PostMapping("/music/getbytype")
    @ResponseBody
    public String getAllByType(String type){
        List<Music> musics = musicMapping.queryByType(type);
        return RestfulUtil.toRestful(musics);
    }

    //音乐增加喜欢接口
    @PostMapping("/music/addlike")
    @ResponseBody
    public String AddLike(Integer musicId){
        Music music = musicMapping.queryById(musicId);
        if (music.getMusicLikeNum() == null || music.getMusicLikeNum() <= 1){
            music.setMusicLikeNum(1);
        }else {
            Integer musicLikeNum = music.getMusicLikeNum();
            music.setMusicLikeNum(musicLikeNum + 1);
        }
        Integer userId = useIdLocal.get();
        if (userId == null){
            return RestfulUtil.toRestful(null,"用户id丢失",404);
        }
        musicMapping.addMusicLike(userId,musicId);

        return RestfulUtil.toRestful(null);
    }

    //音乐删除喜欢接口
    @PostMapping("/music/detelelike")
    @ResponseBody
    public String deteleLike(Integer musicId){
        Music music = musicMapping.queryById(musicId);

        Integer musicLikeNum = music.getMusicLikeNum();
        music.setMusicLikeNum(musicLikeNum - 1);

        Integer userId = useIdLocal.get();
        if (userId == null){
            return RestfulUtil.toRestful(null,"用户id丢失",404);
        }
        musicMapping.deleteMusicLike(userId,musicId);
        return RestfulUtil.toRestful(null);
    }

    //根据id获取音乐是否喜欢
    @PostMapping("/music/getislike/{musicId}")
    @ResponseBody
    public String getIsLike(@PathVariable Integer musicId){
        Music music = musicMapping.queryById(musicId);
        Integer userId = useIdLocal.get();
        if (userId == null){
            return RestfulUtil.toRestful(null,"用户id丢失",404);
        }
        List<Object> objects = musicMapping.queryLike(userId, musicId);
        if (objects.size() > 0){
            return RestfulUtil.toRestful(true);
        }else{
            return RestfulUtil.toRestful(false);
        }

    }

    //获取我喜欢的音乐接口
    @PostMapping("/music/getbymylike")
    @ResponseBody
    public String getByMyLike(){
        Integer userId = useIdLocal.get();
        if (userId == null){
            return RestfulUtil.toRestful(null,"用户id丢失",404);
        }
        List<Music> musics = musicMapping.queryByMyLike(userId);

        return RestfulUtil.toRestful(musics);
    }

    //根据歌单id获取歌曲列表
    @PostMapping("/music/getbymusiclist/{musicListId}")
    @ResponseBody
    public String getByMusicList(@PathVariable Integer musicListId){
        List<Music> musics = musicMapping.queryByMusicListId(musicListId);
        musics.removeAll(Collections.singleton(null));
        return RestfulUtil.toRestful(musics);
    }

    //给歌单添加音乐接口(新增音乐与歌单关联关系)
    @PostMapping("/music/insertbymusiclist")
    @ResponseBody
    public String insertByMusicList(Integer musicId,Integer musicListId){
        musicMapping.insertByMusicList(musicId,musicListId);
        return RestfulUtil.toRestful(null);
    }

    //音乐删除歌单接口(删除音乐与歌单关联关系)
    @PostMapping("/music/detelebymusiclist")
    @ResponseBody
    public String deteleByMusicList(Integer musicId,Integer musicListId){
        musicMapping.deleteByMusicList(musicId,musicListId);
        return RestfulUtil.toRestful(null);
    }

    //音乐搜索接口(删除音乐与歌单关联关系)
    @PostMapping("/music/search")
    @ResponseBody
    public String search(String str){
        if (str == null ||str.equals("")){
            return RestfulUtil.toRestful(null,"请输入关键字",500);
        }
        String[] strings = str.split(" ");

        String sql = "SELECT * FROM music WHERE CONCAT(music_name, music_author) LIKE '%" + strings[0] +"%'" ;
        if (strings.length > 1) {
            for (int i = 0; i < strings.length;i++){
                if (i == 0){
                    continue;
                }
                sql += " AND CONCAT(music_name, music_author) LIKE "+ strings[0];
            };
        }
        List<Music> musics = musicMapping.excutesql(sql);
        return RestfulUtil.toRestful(musics);
    }










}
